package com.googlecode.cswish.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.TagLibrary;

import com.googlecode.cswish.util.BlankObject;
import com.googlecode.cswish.util.ClassUtil;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * use com.espirit.eap.util.TaglibManager
 * 
 * @see org.apache.struts2.views.DefaultTagLibrary
 * @see struts-plugin.xml
 * 
 * @author Jerry.Feng Date: 2008-5-30
 */
@Deprecated
public class TagManager implements TagLibrary {
	
	private static final Logger logger = Logger.getLogger(TagManager.class);
	public static final String STRUTS_COMPONENT_PACKAGE = "org.apache.struts2.components";
	
	private static final TagManager instance = new TagManager();

	private Map<String, Class<? extends Component>> component
		= new HashMap<String, Class<? extends Component>>();
	
	// short name --> full name
	private Map<String, String> templateNameMapping
		= new HashMap<String, String>();
	
	public Object getFreemarkerModels(ValueStack stack, HttpServletRequest req,
            HttpServletResponse res) {
		return new ExStrutsModels(stack, req, res);
	}
	
	public static TagManager getInstance() {
		return instance;
	}

	public void register(String tagName, Class<? extends Component> clazz) {
		component.put(tagName, clazz);
	}
	
	public void registerTemplateMapping(String shortName, String fullName) {
		templateNameMapping.put(shortName, fullName);
	}
	
	// modelField ==> eap/pageLayout/modelField.ftl
	public String getTemplateName(String shortName) {
		return templateNameMapping.get(shortName);
	}
	
	public Class<? extends Component> getComponentClass(String tagName) {
		Class clazz = component.get(tagName);
		if (clazz == null) {
			try {
				clazz = ClassUtil.loadClass(tagName, this.getClass());
				component.put(tagName, clazz);
			} catch (Exception ex) {
				String className;
				if (Character.isLowerCase(tagName.charAt(0))) {
					className = STRUTS_COMPONENT_PACKAGE + '.'
						+ Character.toUpperCase(tagName.charAt(0)) + tagName.substring(1);
				} else {
					className = STRUTS_COMPONENT_PACKAGE + '.' + tagName;
				}
				
				try {
					clazz = ClassUtil.loadClass(className, this.getClass());
					component.put(tagName, clazz);
				} catch (Exception ex2) {
					logger.info("Can't find class: " + className);
				}
			}
		}
		return clazz;
	}

	@Override
	public List<Class> getVelocityDirectiveClasses() {
		return BlankObject.List;
	}

	public String getShortName(String type, boolean smallCase) {
		if (type.startsWith(STRUTS_COMPONENT_PACKAGE)) {
			int beginIndex = STRUTS_COMPONENT_PACKAGE.length() + 1;
			type = Character.toUpperCase(type.charAt(beginIndex)) + type.substring(beginIndex + 1);
		}
		if (smallCase) {
			if (type.endsWith("ield")) {	// Field and field
				type = type.substring(0, type.length() - 5);
			}
			type = type.toLowerCase();
		}
		return type;
	}
}
